<template>
  <div>
    <el-button type="primary" @click="showDialog">BUG提交</el-button>

    <el-dialog title="BUG详情提交" :visible.sync="dialogFormVisible">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        &emsp;开 发 人 员&ensp;
        <el-select v-model="value" placeholder="请选择">
          <el-option
            v-for="item in DeveloperNameList"
            :key="item._id"
            :label="item.username"
            :value="item.username + '|' + item.usertype"
          ></el-option>
        </el-select>
        <p></p>
        <el-form-item label="新增BUG" prop="bugreason">
          <el-input type="text" v-model="ruleForm.bugreason"></el-input>
        </el-form-item>
        <el-form-item label="开始时间" prop="start">
          <el-date-picker v-model="ruleForm.start" type="date" placeholder="开始时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="end">
          <el-date-picker v-model="ruleForm.end" type="date" placeholder="结束时间" />
        </el-form-item>
        <el-upload
          class="upload-demo"
          ref="upload"
          action="/api/uploads"
          name="filename"
          multiple
          
          :on-success="success"
          :file-list="fileList"
          :on-change="change"
        >
          <el-button slot="trigger" size="small" type="primary">点击文件</el-button>
          <el-button style="margin-left: 10px;" size="small" type="success">上传到服务器</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
export default {
  data() {
    return {
      fileList: [],
      selectedList: [], //选择的图片
      uploadList: [], //上传成功的图片
      dialogFormVisible: false,
      ruleForm: {
        DeveloperName: "",
        bugreason: "",
        start: "",
        end: ""
      },
      options: [],
      value: "",

      rules: {
        leavereason: [
          { required: true, message: "BUG描述不能为空", trigger: "blur" },
          {
            pattern: /^.{2,20}$/i,
            message: "长度2到20",
            trigger: "blur"
          }
        ],
        start: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        end: [{ required: true, message: "结束时间不能为空", trigger: "blur" }]
      }
    };
  },

  computed: {
    ...mapState("testInfo", ["DeveloperNameList"]),
    change(file, fileList) {
      //每次改变触发
      console.log(fileList);
      // this.selectedList = fileList;
    },
    usertype() {
      return this.value.split("|");
    }
  },
  methods: {
    ...mapActions("testInfo", ["addBug", "getBugList", "getDeveloperNameList"]),
    showDialog() {
      //弹出一个对话框
      this.dialogFormVisible = true;
    },
    success(res, file, fileList) {
      this.selectedList.push(res.url);
      console.log(res.url);

    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          //   console.log("成功" + valid);
          //清空表单和关闭弹出层
          if (new Date(this.ruleForm.end) >= new Date(this.ruleForm.start)) {
            console.log(this.value); //BUG描述
            this.addBug({
              bugreason: this.ruleForm.bugreason,
              start: this.ruleForm.start,
              end: this.ruleForm.end,
              DeveloperName: this.usertype[0], //开发人员姓名
              usertype: this.usertype[1], //开发人员项目组
              imgurl:this.selectedList.join("|")
            });
            // this.getBugList();
            this.$refs[formName].resetFields();
            this.dialogFormVisible = false;
          } else {
            this.$message.error("结束时间不能小于开始时间");
          }
        } else {
          console.log("errorsubmit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogFormVisible = false;
    }
  },

  created() {
    this.getDeveloperNameList();
  }
};
</script>

<style></style>
